package xcmg.device.service.sale;

import com.github.pagehelper.PageInfo;
import yb.ecp.fast.infra.util.PageHelperPlus;
import org.apache.commons.collections.CollectionUtils;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import xcmg.device.dao.entity.delivery.DeliveryNoteDO;
import xcmg.device.dao.entity.delivery.DeliveryNoteDetailDO;
import xcmg.device.dao.entity.sale.SaleIntentionDetailDO;
import xcmg.device.dao.entity.sale.SaleReverseDO;
import xcmg.device.dao.entity.sale.SaleReverseDetailDO;
import xcmg.device.dao.mapper.SaleIntentionDetailMapper;
import xcmg.device.dao.mapper.delivery.DeliveryNoteDetailMapper;
import xcmg.device.dao.mapper.delivery.DeliveryNoteMapper;
import xcmg.device.dao.mapper.sale.SaleReverseDetailMapper;
import xcmg.device.dao.mapper.sale.SaleReverseMapper;
import xcmg.device.dao.mapper.warehouse.WarehouseAreaLocationDOMapper;
import xcmg.device.dao.mapper.warehouse.WarehouseDeliveryMapper;
import xcmg.device.infra.DateUtil;
import xcmg.device.infra.ErrorCode;
import xcmg.device.infra.ExportUtil;
import xcmg.device.infra.enums.BillDesc;
import xcmg.device.service.GenDocumentService;
import xcmg.device.service.delivery.DeliveryNoteService;
import xcmg.device.service.vo.sale.SaleReverseAddVO;
import xcmg.device.service.vo.sale.SaleReverseDetailVO;
import xcmg.device.service.vo.sale.SaleReverseVO;
import xcmg.device.service.warehouse.CommonService;
import xcmg.device.service.warehouse.WarehouseInboundService;
import yb.ecp.fast.feign.FastGenClient;
import yb.ecp.fast.infra.infra.ActionResult;
import yb.ecp.fast.infra.infra.PageCommonVO;
import yb.ecp.fast.infra.infra.SearchCommonVO;
import yb.ecp.fast.infra.util.ListUtil;
import yb.ecp.fast.infra.util.StringUtil;

import javax.servlet.http.HttpServletResponse;
import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Date;
import java.util.List;

@Service
public class SaleReverseService {

    @Autowired
    private SaleReverseMapper saleReverseMapper;

    @Autowired
    private SaleReverseDetailMapper saleReverseDetailMapper;

    @Autowired
    private SaleReverseDetailService saleReverseDetailService;

    @Autowired
    private GenDocumentService genDocumentService;

    @Autowired
    private FastGenClient fastGenClient;

    @Autowired
    private HttpServletResponse response;

    @Autowired
    private CommonService commonService;

    @Autowired
    private DeliveryNoteMapper deliveryNoteMapper;

    @Autowired
    private DeliveryNoteDetailMapper deliveryNoteDetailMapper;

    @Autowired
    private WarehouseDeliveryMapper warehouseDeliveryMapper;

    @Autowired
    private WarehouseInboundService warehouseInboundService;

    @Autowired
    private SaleIntentionService saleIntentionService;

    @Autowired
    private SaleIntentionDetailMapper saleIntentionDetailMapper;

    @Autowired
    private DeliveryNoteService deliveryNoteService;

    @Autowired
    private WarehouseAreaLocationDOMapper warehouseAreaLocationDOMapper;
    /**
     * 是否冲销。1已冲销
     */
    private static final Integer IS_REVERSE_YES = 1;

    /**
     * 是否已过账。0否1是
     */
    private static final Integer IS_POSTING_YES = 1, IS_POSTING_NO = 0;


    /**
     * @Author: WangKe
     * @Description: 新增冲销入库单
     * @Date: 2018/3/6 0006
     */
    @Transactional(rollbackFor = Exception.class)
    public ErrorCode add(SaleReverseAddVO saleReverseAddVO, String userId, String orgId)throws Exception{
        ActionResult<String> textResult = fastGenClient.textGuid();
        if (textResult.getCode() != ErrorCode.Success.getCode())
        {
            return ErrorCode.IllegalArument;
        }
        SaleReverseVO saleReverseVO = saleReverseAddVO.getHeaderInfo();
        //查找发货通知单
        DeliveryNoteDO deliveryNoteDO = deliveryNoteMapper.findByDeliveryNo(saleReverseVO.getDeliveryNoteOrder(), orgId);
        if(deliveryNoteDO == null){
            throw new Exception("未找到相关联的发货通知单");
        }
        if(deliveryNoteDO.getTicketState() == 1){
            throw new Exception("发货通知单已开票，请走退货流程");
        }
        List<DeliveryNoteDetailDO> noteDetailList = deliveryNoteDetailMapper.list(deliveryNoteDO.getId());
        if(ListUtil.isNullOrEmpty(noteDetailList)){
            throw new Exception("未找到相关联的发货通知单详情记录");
        }

        SaleReverseDO saleReverseDO = new SaleReverseDO();
        BeanUtils.copyProperties(saleReverseVO, saleReverseDO);
        saleReverseDO.setId(textResult.getValue());
        saleReverseDO.setReverseOrder(genDocumentService.getDocumentNo(BillDesc.SaleReverse));
        saleReverseDO.setAgentId(deliveryNoteDO.getPurchaseOrgId());
        saleReverseDO.setAgentName(deliveryNoteDO.getPurchaseOrgName());
        saleReverseDO.setWarehouseCode(deliveryNoteDO.getOutLocation());
        saleReverseDO.setInArea(deliveryNoteDO.getOutArea());
        saleReverseDO.setSapInArea(deliveryNoteDO.getSapOutArea());
        saleReverseDO.setIsPosting(deliveryNoteDO.getIsPosting());
        saleReverseDO.setIsSapReverse(0);
        saleReverseDO.setState(0);
        saleReverseDO.setAuditState(0);
        saleReverseDO.setCreateDate(new Date());
        saleReverseDO.setCreateId(userId);
        saleReverseDO.setOrgId(orgId);
        saleReverseDO.setCreateName(commonService.getUserName(userId));
        boolean success = saleReverseMapper.insert(saleReverseDO)>0;
        if(success){
            //新增冲销入库详情记录
            List<SaleReverseDetailVO> detailVOList = saleReverseAddVO.getDetailList();

            Integer deleteCount = 0;
            if(CollectionUtils.isNotEmpty(detailVOList)){
                for(SaleReverseDetailVO detail : detailVOList){
                    detail.setReverseOrder(saleReverseDO.getReverseOrder());
                    detail.setDeliveryNoteOrder(saleReverseDO.getDeliveryNoteOrder());
                    detail.setRealQty(detail.getAmount());
                    detail.setCreateId(userId);
                    detail.setCreateName(saleReverseDO.getCreateName());
                    detail.setOrgId(orgId);
                    success = saleReverseDetailService.add(detail).getCode()==0;
                    if(!success){
                        throw new Exception("新增冲销入库详情记录失败");
                    }

                    //2019/3/20 此处注释掉的逻辑是销售冲销后要调整对应的发货通知单的相关操作，现在销售冲销SAP过账后调用了发货通知单过账删除接口来代替此功能
                    //且该操作应该放到销售冲销的SAP过账处来执行，不应该在新增冲销单时就执行，冲销单还需要审核
//                    if (updateOrDelDeliveryQty(deliveryNoteDO.getId(), detail.getAmount(), detail.getMaterialNo())) {
//                        deleteCount++;
//                    }
                }
            }

            //更新发货通知单和出库事务单的冲销状态
//            deliveryNoteService.updateReverseState(deliveryNoteDO, IS_REVERSE_YES, orgId);

            // 如果全部删除，发货通知单调为已过账，后续不能过账，否则改为未过账，冲销单过账时发货通知单自动过账
//            updateDeliveryNote(deliveryNoteDO.getId(), deleteCount != detailVOList.size());
        }
        return success? ErrorCode.Success: ErrorCode.Failure;
    }

    /**
     * 更新发货通知单
     *
     * @param deliveryNoteId
     * @param needPost 是否有必要继续过账，如果下面的零件都删除了，就没必要了
     * @return
     */
    private boolean updateDeliveryNote(String deliveryNoteId, boolean needPost) {
        DeliveryNoteDO noteDO = new DeliveryNoteDO();
        noteDO.setSapSaleOrder(null);
        noteDO.setSapOutBill(null);
        noteDO.setMaterialsNo(null);
        noteDO.setId(deliveryNoteId);
        noteDO.setIsPosting(needPost ? IS_POSTING_NO : IS_POSTING_YES);
        return deliveryNoteMapper.update4Reverse(noteDO)>0;
    }


    /**
     * 更新或者删除发货通知单对应零件，如果冲销数量等于发货通知单已出库数量，则删除发货通知单的零件
     * @param deliveryId
     * @param reverseQty
     * @param materialsNo
     * @return
     */
    private boolean updateOrDelDeliveryQty(String deliveryId, BigDecimal reverseQty, String materialsNo) {

        boolean deleteFlag = false;
        DeliveryNoteDetailDO detailDO = deliveryNoteDetailMapper.itemByMaterialsNo(deliveryId, materialsNo);

        if (reverseQty.compareTo(detailDO.getInventoryOutAmount()) >= 0) {
            deliveryNoteDetailMapper.deleteByPrimaryKey(detailDO.getId());
            deleteFlag = true;
        } else {
            detailDO.setAmountAfterAdjust(subFormat(detailDO.getAmountAfterAdjust(), reverseQty));
            detailDO.setInventoryOutAmount(subFormat(detailDO.getInventoryOutAmount(), reverseQty));
            detailDO.setPackNumber(subFormat(detailDO.getPackNumber(), reverseQty));
            detailDO.setSendNumber(subFormat(detailDO.getSendNumber(), reverseQty));
            detailDO.setDeliveryQty(subFormat(detailDO.getDeliveryQty(), reverseQty));
            detailDO.setSendQty(subFormat(detailDO.getSendQty(), reverseQty));
            deliveryNoteDetailMapper.update(detailDO);
        }
        return deleteFlag;
    }

    /**
     *
     * @param subtraction 减数
     * @param subtracted 被减数
     * @return
     */
    private BigDecimal subFormat(BigDecimal subtraction, BigDecimal subtracted) {

        if (subtraction.compareTo(subtracted) < 0) {
            return BigDecimal.ZERO;
        }

        return subtraction.subtract(subtracted);
    }

//    private void deteNoteSapRomve(String deliveryNoteId,String orgid){
//        deliveryNoteService.sapRemove(deliveryNoteId, orgid);
//    }

    /**
     * @Author: WangKe
     * @Description: 审核销售冲销单
     * @Date: 2018/3/21 0021
     */
    @Transactional(rollbackFor = Exception.class)
    public ErrorCode audit(SaleReverseVO saleReverseVO, String orgId, String userId)throws Exception{
        if(saleReverseVO == null || saleReverseVO.getAuditState() == 0){
            return ErrorCode.IllegalArument;
        }
        SaleReverseDO saleReverseDO = saleReverseMapper.selectByPrimaryKey(saleReverseVO.getId());
        if(saleReverseDO == null){
            return ErrorCode.IllegalArument;
        }

        //查找发货通知单
        DeliveryNoteDO deliveryNoteDO = deliveryNoteMapper.findByDeliveryNo(saleReverseDO.getDeliveryNoteOrder(), orgId);
        if(deliveryNoteDO == null){
            throw new Exception("未找到相关联的发货通知单");
        }
        boolean success;
        if(saleReverseVO.getAuditState() == 1){ //审核通过时，执行冲销动作
            if(deliveryNoteDO.getTicketState() == 1){
                throw new Exception("发货通知单已开票，请走退货流程");
            }else if(deliveryNoteDO.getIsPosting() == 1){
                if(StringUtil.isNullOrEmpty(deliveryNoteDO.getSapOutBill())){
                    throw new Exception("发货通知单SAP外向发货单号为空");
                }
//                int state = sapCommonService.isDeliveryTicket(deliveryNoteDO.getSapOutBill(), orgId);
//                if(state == 1){
//                    throw new Exception("发货通知单已开票，请走退货流程");
//                }
            }else if(deliveryNoteDO.getIsPosting() != 1){
                throw new Exception("发货通知单未过账");
            }
            List<SaleReverseDetailDO> reverseDetailDOList = saleReverseDetailMapper.findByReverseOrderAndOrgId(saleReverseDO.getReverseOrder(), orgId);
            if(ListUtil.isNullOrEmpty(reverseDetailDOList)){
                throw new Exception("未找到冲销入库单详情记录");
            }
            if(deliveryNoteDO.getIsReverse().equals(1)){
                throw new Exception("该发货通知单已冲销");
            }
            //判断冲销类型：如果是部分冲销的话
            if(saleReverseDO.getReverseType() != null && saleReverseDO.getReverseType() == 1){
                //同步SAP整单删除接口
//                deteNoteSapRomve(deliveryNoteDO.getId(), orgId);
//                ErrorCode code = sapCommonService.sapSaleReverse(saleReverseDO.getDeliveryNoteOrder(), orgId);
//                //如果调用接口成功
//                if(!code.equals(ErrorCode.Success)){
//                    throw new Exception("同步SAP整单删除接口失败");
//                }
                //遍历详情
                for(SaleReverseDetailDO saleReverseDetailDO:reverseDetailDOList){
                    //更新已出库数量
                    DeliveryNoteDetailDO deliveryNoteDetailDO=deliveryNoteDetailMapper.itemByMaterialsNo(deliveryNoteDO.getId(),saleReverseDetailDO.getMaterialNo());
                    if(deliveryNoteDetailDO==null){
                        throw new Exception("无查到发货详情！");
                    }
                    deliveryNoteDetailDO.setInventoryOutAmount(deliveryNoteDetailDO.getInventoryOutAmount().subtract(saleReverseDetailDO.getAmount()));
                    if(deliveryNoteDetailDO.getInventoryOutAmount().equals(BigDecimal.ZERO)){
                        deliveryNoteDetailDO.setStatus("0");
                    }else {
                        deliveryNoteDetailDO.setStatus("1");
                    }
                    success = deliveryNoteDetailMapper.update(deliveryNoteDetailDO)>0;
                    if(!success){
                        throw new Exception("更新已出库数量出错！");
                    }
                    //更新销售意向单的出库数量
                    SaleIntentionDetailDO saleIntentionDetailDO=saleIntentionDetailMapper.selectByNo(deliveryNoteDO.getSaleNo(),saleReverseDetailDO.getMaterialNo());
                    if(saleIntentionDetailDO==null){
                        throw new Exception("未查到销售详情！");
                    }
                    saleIntentionDetailDO.setInventoryOutAmount(saleIntentionDetailDO.getInventoryOutAmount().subtract(saleReverseDetailDO.getAmount()));
                    if(saleIntentionDetailDO.getInventoryOutAmount().subtract(saleReverseDetailDO.getAmount()).equals(0)){
                        saleIntentionDetailDO.setInventoryOutStatus("0");
                    }else {
                        saleIntentionDetailDO.setInventoryOutStatus("1");
                    }
                    saleIntentionDetailMapper.update(saleIntentionDetailDO);
                    //自动调用结单功能 billNo:发货通知单
                    deliveryNoteService.manualFinish(saleReverseDetailDO.getDeliveryNoteOrder(), saleReverseDetailDO.getMaterialNo(), orgId);
                }
                //销售意向单状态修改
                success = saleIntentionService.updateSaleOutStatus(deliveryNoteDO, orgId);
                if(!success){
                    throw new Exception("更新销售意向单出库状态失败");
                }
                //同步SAP，重新生成订单信息
                DeliveryNoteDO getIdByBillNo = deliveryNoteMapper.findByDeliveryNo(saleReverseDO.getDeliveryNoteOrder(), orgId);
//                boolean sapNoPostOrg = sapCommonService.sapNoPostOrg(orgId);
//                if(!sapNoPostOrg){
//                    ErrorCode ret=deliveryNoteService.postSap(getIdByBillNo.getId(),orgId,userId,saleReverseDO.getAuditUserName());
//                    if(!ret.equals(ErrorCode.Success)){
//                        //返回前端
//                        throw new Exception(ret.getDesc());
//                    }
//                }
                saleReverseDO.setIsSapReverse(1);
            }else{
                //查找所有部分出库或者全部出库的详情记录
                List<SaleReverseDetailDO> dataList = new ArrayList<>();
                for(SaleReverseDetailDO reverseDetailDO : reverseDetailDOList){
                    if(reverseDetailDO.getAmount().compareTo(BigDecimal.ZERO) > 0){
                        dataList.add(reverseDetailDO);
                    }
                }
                if(ListUtil.isNullOrEmpty(dataList)){ //零件未出库时，状态直接标记为已完成
                    saleReverseDO.setState(2);

                    success = saleReverseDetailMapper.updateStateByOrder(saleReverseDO.getReverseOrder(), 2, orgId)>0;
                    if(!success){
                        throw new Exception("更新冲销单详情记录失败");
                    }
                }

                //更新发货通知单和出库事务单的冲销状态
                success = deliveryNoteService.updateReverseState(deliveryNoteDO, 2, orgId);
                if(!success){
                    throw new Exception("更新发货通知单和出库事务单的冲销状态失败");
                }

                //如果零件部分或者全部出库，生成入库事务单
                if(saleReverseDO.getState() != 2){
                    success = warehouseInboundService.saveBySaleReverseIn(saleReverseDO, reverseDetailDOList, userId, orgId);
                    if(!success){
                        throw new Exception("生成入库事务单失败");
                    }
                }

                //销售意向单状态修改
                success = saleIntentionService.updateBySaleReverse(deliveryNoteDO, orgId);
                if(!success){
                    throw new Exception("更新销售意向单发货数量和状态失败");
                }
            }
        }else{
            //更新发货通知单和出库事务单的冲销状态
            deliveryNoteService.updateReverseState(deliveryNoteDO, 3, orgId);
        }
        saleReverseDO.setAuditState(saleReverseVO.getAuditState());
        saleReverseDO.setAuditNote(saleReverseVO.getAuditNote());
        saleReverseDO.setAuditUserId(userId);
        saleReverseDO.setAuditUserName(commonService.getUserName(userId));
        saleReverseDO.setAuditDate(new Date());
        success = saleReverseMapper.updateByPrimaryKeySelective(saleReverseDO)>0;
        return success?ErrorCode.Success:ErrorCode.Failure;
    }

    /**
     * @Author: WangKe
     * @Description: 删除冲销入库单据
     * @Date: 2018/3/6 0006
     */
    @Transactional(rollbackFor = Exception.class)
    public ErrorCode delete(List<String> ids, String orgId)throws Exception{
        int num = saleReverseMapper.findNumForNotAudit(ids);
        if(num > 0){
            return ErrorCode.SaleReverseRemoveError;
        }
        List<String> orderList = saleReverseMapper.findOrderList(ids);
        List<String> deliveryOrderList = saleReverseMapper.findDeliveryOrderList(ids);
        boolean success = saleReverseMapper.remove(ids)>0;
        //删除入库详情记录
        if(success && CollectionUtils.isNotEmpty(orderList)){
            success = saleReverseDetailMapper.removeByInOrderList(orderList, orgId)>0;
            if(!success){
                throw new Exception("删除冲销入库详情记录失败");
            }

            //更新发货通知单冲销状态
            success = deliveryNoteMapper.updateReverseState(deliveryOrderList, 0, orgId)>0;
            if(!success){
                throw new Exception("更新发货通知单冲销状态失败");
            }

            //更新出库事务单冲销状态
            success = warehouseDeliveryMapper.updateReverseState(deliveryOrderList, 0, orgId)>0;
            if(!success){
                throw new Exception("更新出库事务单冲销状态失败");
            }
        }
        return success? ErrorCode.Success: ErrorCode.WarehouseIdError;
    }

    /**
     * @Author: WangKe
     * @Description: 查找冲销入库单据列表
     * @Date: 2018/3/6 0006
     */
    public PageCommonVO list(SearchCommonVO<SaleReverseVO> condition) {
        PageCommonVO<SaleReverseVO> pageCommonVO = new PageCommonVO();
        PageHelperPlus.orderBy("create_date desc");
        PageHelperPlus.startPage(condition.getPageNum(), condition.getPageSize());
        List<SaleReverseDO> doList = saleReverseMapper.list(condition.getFilters());
        List<SaleReverseVO> voList = new ArrayList<SaleReverseVO>();
        for (SaleReverseDO entity : doList) {
            SaleReverseVO model = new SaleReverseVO();
            BeanUtils.copyProperties(entity, model);
            voList.add(model);
        }
        pageCommonVO.setPageInfo(new PageInfo(doList));
        pageCommonVO.setPageInfoList(voList);
        return pageCommonVO;
    }

    public List<SaleReverseDO> list(SaleReverseVO vo){
        return saleReverseMapper.list(vo);
    }

    /**
     * @Author: WangKe
     * @Description: 根据主键或者业务单号查找记录
     * @Date: 2018/3/6 0006
     */
    public SaleReverseAddVO findByPK(String id, String type){
        SaleReverseAddVO model = new SaleReverseAddVO();
        SaleReverseDO entity;
        if("id".equals(type)){
            entity = saleReverseMapper.selectByPrimaryKey(id);
        }else{
            entity = saleReverseMapper.selectByReverseOrder(id);
        }
        if(entity == null){
            return null;
        }

        List<SaleReverseDetailDO> doList = saleReverseDetailMapper.findByReverseOrderForLocationUser(Arrays.asList(entity.getReverseOrder()));
        List<SaleReverseDetailVO> voList = new ArrayList<SaleReverseDetailVO>();
        if(CollectionUtils.isNotEmpty(doList)){
            SaleReverseDetailVO saleReverseDetailVO;
            for(SaleReverseDetailDO m:doList){
                saleReverseDetailVO = new SaleReverseDetailVO();
                BeanUtils.copyProperties(m, saleReverseDetailVO);

                String loactionCode = saleReverseDetailVO.getStockLocation();
                if (StringUtils.isNotBlank(loactionCode)){
                    saleReverseDetailVO.setLocationDesc(warehouseAreaLocationDOMapper.getLocationDesc(loactionCode));
                }
                voList.add(saleReverseDetailVO);
            }
        }
        SaleReverseVO saleReverseVO = new SaleReverseVO();
        BeanUtils.copyProperties(entity, saleReverseVO);

        model.setHeaderInfo(saleReverseVO);
        model.setDetailList(voList);
        return model;
    }

    public void export(List<String> ids)throws Exception{
        invokeExport(saleReverseMapper.findByIds(ids));
    }

    /**
     * @Author: WangKe
     * @Description: 导出冲销入库记录列表
     * @Date: 2018/3/6 0007
     */
    public void exportAll(SaleReverseVO condition)throws Exception{
        Integer count = saleReverseMapper.findCountForExportAll(condition);
        List<SaleReverseDO> dataList = new ArrayList<>();
        int times = count/500+1;
        for(int i=0; i<times; i++){
            PageHelperPlus.startPage(i + 1, 500, "create_date desc");
            dataList.addAll(saleReverseMapper.list(condition));
        }
        invokeExport(dataList);
    }

    /**
     * @Author: WangKe
     * @Description: 导出冲销入库记录列表
     * @Date: 2018/3/6 0006
     */
    private void invokeExport(List<SaleReverseDO> doList)throws Exception{
        String title = "冲销入库数据导出";
        String[] rowsName = new String[]{"序号","冲销入库单号","发货通知单号","代理商名称","仓库编码",
                "入库状态", "审核状态", "发货通知单是否过账", "SAP是否冲销成功", "创建人", "制单时间", "备注"};
        List<Object[]>  dataList = new ArrayList<Object[]>();
        Object[] objs;
        for (int i = 0; i < doList.size(); i++) {
            SaleReverseDO exportDO = doList.get(i);
            objs = new Object[rowsName.length];
            objs[0] = i+1;
            objs[1] = exportDO.getReverseOrder();
            objs[2] = exportDO.getDeliveryNoteOrder();
            objs[3] = exportDO.getAgentName();
            objs[4] = exportDO.getWarehouseCode();
            objs[5] = getState(exportDO.getState());
            objs[6] = getAuditState(exportDO.getAuditState());
            objs[7] = (exportDO.getIsPosting()==null || exportDO.getIsPosting()==0)?"否":"是";
            objs[8] = (exportDO.getIsSapReverse()==null || exportDO.getIsSapReverse()==0)?"否":"是";
            objs[9] = exportDO.getCreateName();
            objs[10] = DateUtil.formatDateTime(exportDO.getCreateDate());
            objs[11] = exportDO.getNote();
            dataList.add(objs);
        }
        ExportUtil exportUtil = new ExportUtil(title, rowsName, dataList);
        exportUtil.export(response);
    }

    private String getState(Integer state){
        if(state == null){
            state = 0;
        }
        if(state == 0 ){
            return "未入库";
        }else if(state == 1 ){
            return "部分入库";
        }else if(state == 2 ){
            return "已入库";
        }else{
            return "强制结单";
        }
    }

    private String getAuditState(Integer state){
        if(state == null){
            state = 0;
        }
        if(state == 0 ){
            return "待审核";
        }else if(state == 1 ){
            return "审核通过";
        }else{
            return "审核拒绝";
        }
    }
}
